import QtQuick
import QtQuick.Controls

Rectangle {
    width: 400
    height: 300
    color: "lightgray"

    Button {
        text: "点击显示动态菜单"
        anchors.centerIn: parent
        onClicked: dynamicMenu.popup()
    }

    Menu {
        id: dynamicMenu
        
        Instantiator {
            model: ["选项1", "选项2", "选项3"]
            delegate: MenuItem {
                text: modelData
                onTriggered: console.log("选择了:", modelData)
            }
            onObjectAdded: function(index, object) {
                dynamicMenu.insertItem(index, object)
            }
            onObjectRemoved: function(index, object) {
                dynamicMenu.removeItem(object)
            }
        }
    }
}
